const Koa = require('koa')
const router = require('koa-router')()
const bodyParser = require('koa-bodyparser')
const fs = require('fs')
const {appendFile} = require("./service");

const app = new Koa()
app.use(bodyParser())

router.get('/', async (ctx) => {
    ctx.type = 'text/html;charset=utf-8'
    ctx.body = fs.createReadStream('./index.html')
})
router.post('/getFileToLocal', async (ctx) => {
    const {type,fileStr} = ctx.request.body
    let promiseArr = fileStr.split(',').map(v=>appendFile(v,type))
    try{
        const result = await Promise.allSettled(promiseArr)
        console.log(result.map(v=>v.status))
        ctx.body = {
            message:'文件生成成功',
            code:200
        }
    }catch (e) {
        ctx.body = {
            message:'文件生成失败',
            code:500
        }
    }


})
app.use(router.routes());
app.listen(3000,()=>{
    console.log('http://localhost:3000')
})
